import { useCallback, useEffect } from "react"
import { useTranslation } from "react-i18next"
import { INIT_LANG } from '@/config'

const useLanguage = () => {
  const lang = localStorage.getItem('language') || INIT_LANG
  const { i18n } = useTranslation()
  const changeLanguage = useCallback((code) => {
    localStorage.setItem('language', code)
    i18n.changeLanguage(code)
  }, [i18n])
  useEffect(() => {
    changeLanguage(lang)
  }, [changeLanguage, lang])
  return {
    initLang: lang,
    changeLanguage
  }
}

export default useLanguage